from django.shortcuts import render, redirect
from django.http import HttpResponse,HttpResponseRedirect
# Create your views here.
from django.urls import reverse


def getTest1(request):
    return render(request,'booktest/getTest1.html')

def getTest2(request):
    a = request.GET['a']
    b = request.GET['b']
    context = {
        'a':a,
        'b':b,
    }
    return render(request,'booktest/getTest2.html',context)

def getTest3(request):
    a = request.GET.getlist('a')
    b = request.GET['b']
    context = {
        'a':a,
        'b':b,
    }
    return render(request,'booktest/getTest3.html',context)

def postTest1(request):
    return render(request,'booktest/posttest1.html')

def postTest2(request):
    name = request.POST['uname']
    passwd = request.POST['upasswd']
    gender = request.POST['ugender']
    hobby = request.POST.getlist('uhobby')
    context = {
        'name':name,
        'passwd':passwd,
        'gender': gender,
        'hobby': hobby,

    }
    return render(request,'booktest/posttest2.html',context)

def cookieTest(request):
    response = HttpResponse('cookie')
    # if 't1' in request.COOKIES.keys():
    if request.COOKIES.__contains__('t1'):
        response.write(request.COOKIES['t1'])
    # response.set_cookie('t1','abc')
    return response

def redTest1(request):
    # return HttpResponseRedirect('/redTest2/')
    return redirect('/redTest2/')
def redTest2(request):
    return HttpResponse('重定向页面')

def index(request):
    uname = request.session.get('uname')
    context = {'uname':uname}
    return render(request,'booktest/index.html',context)
    # return HttpResponse('index')
def login(request):
    return render(request,'booktest/login.html')

def logout(request):
    # request.session.clear()#清空session的值6cca00f4c75b0920edc712d8d2c98ce6538c14fd:{}
    # request.session.flush()#清空session，清空session的key和值
    del request.session['uname']#清空session的值，结果493a47edfe90892d78e8e919a05905bfe5fa411f:{"_session_expiry":0}
    del request.session['passwd']
    return redirect(reverse('booktest:index'))

def login_handle(request):
    request.session['uname'] = request.POST.get('uname')
    request.session['passwd'] = request.POST.get('passwd')
    request.session.set_expiry(0)
    return redirect(reverse('booktest:index'))